/**
 * 
 */
package com.iwords.api.web.base;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * @author       BOLIN.HBC 
 * @time         2011-3-19    
 * @description  所有Action的基类       
 */
public abstract class BaseAction<T> extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	protected final Log log = LogFactory.getLog(this.getClass());

	public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		try {
			doProcess(req, resp);
		} catch (JSONException e) {
			log.error("JASON ERROR", e);
		}

	}

	public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		try {
			doProcess(req, resp);
		} catch (JSONException e) {
			log.error("JASON ERROR", e);
		}

	}

	public abstract void doProcess(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException, JSONException;

	public abstract T initEntity(HttpServletRequest req);

	public void jsonPrint(HttpServletResponse res, JSONObject jsobObject) throws IOException, JSONException {
		
		res.getWriter().print(jsobObject.toString());
		res.getWriter().flush();
	}

}
